package com.heh.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.heh.mybatisplus.mapper.EmpIdMapper;
import com.heh.mybatisplus.model.EmpId;
import com.heh.mybatisplus.service.EmpIdService;
import com.heh.mybatisplus.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @项目名： spring-emq
 * @包名： com.heh.mybatisplus.service.impl
 * @文件名: EmpIdServiceImpl
 * @创建者: liufudong@szyh-smart.com
 * @创建时间: 2024/10/20 11:36
 * @描述：
 */
@Slf4j
@Service
public class EmpIdServiceImpl extends ServiceImpl<EmpIdMapper, EmpId>
        implements EmpIdService {
    @Autowired
    private ObjectMapper objectMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveOrUpdateList(List<EmpId> entities) {
        // 将有 ID 的分到 update 列表，没 ID 的分到 insert 列表
        List<EmpId> toUpdate = entities.stream().filter(entity -> ObjectUtils.isNotEmpty(entity.getId()))
                .collect(Collectors.toList());
        List<EmpId> toInsert = entities.stream().filter(entity -> ObjectUtils.isEmpty(entity.getId()))
                .collect(Collectors.toList());
        boolean insertBoolean = true;
        boolean updateBoolean = true;
        // 批量插入
        if (!toInsert.isEmpty()) {
            insertBoolean = this.saveBatch(toUpdate);
            log.info("EmpIdServiceImpl批量插入:{}", insertBoolean);
        }
        // 批量更新
        if (!toUpdate.isEmpty()) {
            updateBoolean = this.updateBatchById(toUpdate);
            log.info("EmpIdServiceImpl批量更新:{}", updateBoolean);
        }
        return insertBoolean && updateBoolean;
    }
    @Override
    public void batchInsertOrUpdateEmpId(List<EmpId> empIdList) {
        baseMapper.batchInsertOrUpdateEmpId(empIdList);
    }

    @Override
    public void batchInsertOrUpdateEmpId2(List<EmpId> empIdList) throws JsonProcessingException {
        baseMapper.batchInsertOrUpdateEmpId2(empIdList);
        log.info("empIdList==batchInsertOrUpdateEmpId2===={}",objectMapper.writeValueAsString(empIdList));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<EmpId> saveOrUpdateList2(List<EmpId> entities) throws JsonProcessingException {
        // 将有 ID 的分到 update 列表，没 ID 的分到 insert 列表
        List<EmpId> toUpdate = entities.stream()
                .filter(entity -> ObjectUtils.isNotEmpty(entity.getId()))
                .collect(Collectors.toList());

        log.info("============toUpdate{}", objectMapper.writeValueAsString(toUpdate));
        List<EmpId> toInsert = entities.stream()
                .filter(entity -> ObjectUtils.isEmpty(entity.getId()))
                .collect(Collectors.toList());

        log.info("============toInsert{}", objectMapper.writeValueAsString(toInsert));
        // 批量插入，返回生成的主键
        if (!toInsert.isEmpty()) {
            baseMapper.insertBatch(toInsert);
        }

        // 批量更新
        if (!toUpdate.isEmpty()) {
//            baseMapper.updateBatch(toUpdate);
            updateBatchById(toUpdate);
//            baseMapper.updateBatch();

        }

        // 返回包含主键和更新信息的对象列表
        return entities;
    }
}
